import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:tender_flutter/constant/app_constant.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/common/api/api_common.dart';
import 'package:tender_flutter/page/home/api/api_home.dart';
import 'package:tender_flutter/page/home/model/city_entity.dart';
import 'package:tender_flutter/page/home/model/common_tag_model.dart';
import 'package:tender_flutter/page/home/tender_list_page.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/drop_down_menu/drop_down_address_widget.dart';
import 'package:tender_flutter/widgets/drop_down_menu/drop_dpwm_list_widget.dart';
import 'package:tender_flutter/widgets/search_app_bar.dart';

class PPPSearchPage extends StatefulWidget {
  @override
  _PPPSearchPageState createState() => _PPPSearchPageState();
}

class _PPPSearchPageState extends State<PPPSearchPage> {
  List<String> _dropDownHeaderItemStrings = ['全国', '金额', '时间'];
  CityEntity _selectCityModel;
  CommonTagModel _selectMoneyModel;
  CommonTagModel _selectTimeModel;
  String _keyword;

  GZXDropdownMenuController _dropdownMenuController =
      GZXDropdownMenuController();

  GlobalKey<TenderListPageState> _listKey = GlobalKey();

  Future<List<CommonTagModel>> _filterMoneyFuture;

  Future<List<CommonTagModel>> _filterTimesFuture;

  GlobalKey _scaffoldKey = GlobalKey<ScaffoldState>();
  GlobalKey _stackKey = GlobalKey();

  // ///刷新数据

  /// 获取筛选金额
  Future<List<CommonTagModel>> _getMoneys() async {
    return ApiHome.getFilterMoneyDatas();
  }

  /// 获取时间
  Future<List<CommonTagModel>> _getTimes() async {
    return ApiHome.getFilterTimesDatas();
  }

  /// 数据刷新
  void _refresh() {
    TenderListPageState listPageState = _listKey.currentState;
    listPageState.areaCode = _selectCityModel != null ? _selectCityModel.id : 0;
    listPageState.beginTime =
        _selectTimeModel != null ? _selectTimeModel.value.split(',')[0] : "";
    listPageState.endTime =
        _selectTimeModel != null ? _selectTimeModel.value.split(',')[1] : "";
    listPageState.minMoney =
        _selectMoneyModel != null ? _selectMoneyModel.value.split(',')[0] : "";
    listPageState.maxMoney =
        _selectMoneyModel != null ? _selectMoneyModel.value.split(',')[1] : "";
    listPageState.keyword = _keyword;

    listPageState.updateData(true);
  }

  @override
  void initState() {
    super.initState();
    _filterTimesFuture = _getTimes();
    _filterMoneyFuture = _getMoneys();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body: _initBody(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return SearchAppBar(
      context,
      searchCallback: (value) {
        // 键盘消失
        FocusScope.of(context).requestFocus(FocusNode());
        _keyword = value;
        _refresh();
      },
    );
  }

  /// 筛选菜单
  Widget _initBody() {
    return Stack(
      key: _stackKey,
      children: <Widget>[
        Column(
          children: <Widget>[
            // 下拉菜单头部
            GZXDropDownHeader(
              height: ScreenAdapter.height(90),
              // 下拉的头部项，目前每一项，只能自定义显示的文字、图标、图标大小修改
              items: [
                GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
                GZXDropDownHeaderItem(_dropDownHeaderItemStrings[1]),
                GZXDropDownHeaderItem(
                  _dropDownHeaderItemStrings[2],
                ),
              ],
              // GZXDropDownHeader对应第一父级Stack的key
              stackKey: _stackKey,
              // controller用于控制menu的显示或隐藏
              controller: _dropdownMenuController,
              // 当点击头部项的事件，在这里可以进行页面跳转或openEndDrawer
              onItemTap: (index) {},
              // 文字样式
              style: TextStyle(
                  color: ThemeColors.colorType1,
                  fontSize: ScreenAdapter.sp(28)),
              // 下拉时文字样式
              dropDownStyle: TextStyle(
                fontSize: ScreenAdapter.sp(28),
                color: ThemeColors.colorType6,
              ),
              // 图标颜色
              iconColor: ThemeColors.colorType7,
              // 下拉时图标颜色
              iconDropDownColor: ThemeColors.colorType6,
            ),
            Expanded(
              child: TenderListPage(
                tenderType: AppConstant.TENDERTYPE_PPP,
                key: _listKey,
                keyword: _keyword,
                areaCode: 0,
              ),
            ),
          ],
        ),
        // 下拉菜单
        GZXDropDownMenu(
          // controller用于控制menu的显示或隐藏
          controller: _dropdownMenuController,
          // 下拉菜单显示或隐藏动画时长
          animationMilliseconds: 300,
          dropdownMenuChanging: (isShow, index) {},
          dropdownMenuChanged: (isShow, index) {},
          // 下拉菜单，高度自定义，你想显示什么就显示什么，完全由你决定，你只需要在选择后调用_dropdownMenuController.hide();即可
          menus: [
            GZXDropdownMenuBuilder(
                dropDownHeight: 40 * 8.0,
                dropDownWidget: FutureBuilder(
                    future: ApiCommon.getCityDatas(),
                    builder: (BuildContext context,
                        AsyncSnapshot<List<CityEntity>> snapshot) {
                      if (snapshot.hasData) {
                        return DropMenuAddressWidget(
                          cityDatas: snapshot.data,
                          selectCityCallback:
                              (levelIndex, itemIndex, cityModel) {
                            if (levelIndex == 1) {
                              _selectCityModel = cityModel;
                              _dropDownHeaderItemStrings[0] = cityModel.title;
                              _dropdownMenuController.hide();
                              _refresh();
                            }
                            setState(() {});
                          },
                        );
                      } else {
                        return Container();
                      }
                    })),
            GZXDropdownMenuBuilder(
                dropDownHeight: ScreenAdapter.height(80) * 6.0,
                dropDownWidget: FutureBuilder(
                    future: _filterMoneyFuture,
                    builder: (BuildContext context,
                        AsyncSnapshot<List<CommonTagModel>> snapshot) {
                      if (snapshot.hasData) {
                        return DropDownListWidget(
                          listDatas: snapshot.data,
                          selectListItemCallback: (itemIndex, tagModel) {
                            _selectMoneyModel = tagModel;
                            _dropDownHeaderItemStrings[1] =
                                _selectMoneyModel.title == '全部'
                                    ? '金额'
                                    : _selectMoneyModel.title;
                            _dropdownMenuController.hide();
                            setState(() {});
                            _refresh();
                          },
                        );
                      } else {
                        return Container();
                      }
                    })),
            GZXDropdownMenuBuilder(
                dropDownHeight: ScreenAdapter.height(80) * 6,
                dropDownWidget: FutureBuilder(
                    future: _filterTimesFuture,
                    builder: (BuildContext context,
                        AsyncSnapshot<List<CommonTagModel>> snapshot) {
                      if (snapshot.hasData) {
                        return DropDownListWidget(
                          listDatas: snapshot.data,
                          selectListItemCallback: (itemIndex, tagModel) {
                            _selectTimeModel = tagModel;
                            _dropDownHeaderItemStrings[2] =
                                _selectTimeModel.title == '全部'
                                    ? '时间'
                                    : _selectTimeModel.title;
                            _dropdownMenuController.hide();
                            setState(() {});
                            _refresh();
                          },
                        );
                      } else {
                        return Container();
                      }
                    })),
          ],
        ),
      ],
    );
  }
}
